import { baseURL, request } from "../../api/api"

// pages/todolist/todolist.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    comments: [],
    obj: {},
    title: '',
    content: ''
  },
  updateItem(e) {
    const item = e.target.dataset.item;
    const index = e.target.dataset.index;
    wx.showModal({
      title: item.title,
      content: item.content,
      showCancel: true,
      editable: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '修改',
      confirmColor: '#3CC51F',
      success: async (result) => {
        if (result.confirm) {
          console.log(result)
          item.content = result.content;
          let res = await request({
            url: "/react/updatecomment",
            method: "post",
            data: item,
          })
          if (res.data.code == 200) {
            this.data.comments.splice(index, 1, item);
            this.setData({
              comments: this.data.comments
            })
          }
        }
      },
      fail: () => { },
      complete: () => { }
    });
  },
  deleteItem(e) {
    console.log(e)
    const item = e.target.dataset.item;
    const index = e.target.dataset.index;
    wx.showModal({
      title: '友情提示',
      content: '亲爱的,你真的要删除吗???',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: async (result) => {
        if (result.confirm) {
          let res = await request({
            url: "/react/deletecomment",
            method: "post",
            data: {
              _id: item._id
            }
          })
          if (res.data.code == 200) {
            this.data.comments.splice(index, 1)
            this.setData({
              comments: this.data.comments
            })
          }
        }
      },
      fail: () => { },
      complete: () => { }
    });
  },
  async todoSubmit() {
    console.log(this.data.title, this.data.content)
    if (this.data.title && this.data.content) {
      let res = await request({
        url: '/react/addcomment',
        method: 'post',
        data: {
          title: this.data.title,
          content: this.data.content
        }
      })
      if (res.data.code == 200) {
        this.getCommentList()
        this.resetData()
      }
    } else {
      wx.showToast({
        title: '请先填写数据',
        icon: 'none'
      })
    }
  },
  resetData() {
    this.setData({
      title: '',
      content: ''
    })
  },
  getCommentList() {
    wx.showLoading({
      title: '请求中',
    })
    wx.request({
      url: baseURL + '/react/getcommentlist',
      method: "post",
      data: {},
      success: res => {
        console.log(res)
        wx.showToast({
          title: '请求成功',
          icon: 'none'
        })
        this.setData({
          comments: res.data.result
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCommentList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})